package 剑指offer_leetcode;

import java.util.Stack;

/**
 * JZ30包含min函数的栈
 */
public class 剑指Offer30_包含min函数的栈 {
    class MinStack {
        Stack<Integer> s1;
        Stack<Integer> s2;

        /** initialize your data structure here. */
        public MinStack() {
            this.s1 = new Stack();
            this.s2 = new Stack();
        }

        public void push(int x) {
            s1.push(x);
            if(s2.isEmpty()||s2.peek()>x)
                s2.push(x);
            else
                s2.push(s2.peek());
        }

        public void pop() {
            s1.pop();
            s2.pop();
        }

        public int top() {
            return s1.peek();
        }

        public int min() {
            return s2.peek();
        }
    }
}
